from urllib.request import urlopen  ##(负责打开浏览url内的html 文本)
import ssl
import json
import requests

from aip import AipOcr

ssl._create_default_https_context = ssl._create_unverified_context


# 请求百度OCR接口类
class httpUtils:
    def __int__(self):
        pass

    def getBaiduOCRData(self, file):
        APP_ID = '25856883'             #我的申请的百度云账号
        API_KEY = 'zaPb5qRGj3EYY3goYOs0DFC7'
        SECRET_KEY = 'o0sxXPt4RgTmXRQ3mQERc7B3G6tXjfnc'
        f = get_file_content(file)
        options = {}
        # options["recognize_granularity"] = "big"
        # options["language_type"] = "CHN_ENG"
        options["detect_direction"] = "true"
        options["detect_language"] = "true"
        options["vertexes_location"] = "true"
        # options["probability"] = "true"
        res = AipOcr(APP_ID, API_KEY, SECRET_KEY).basicGeneral(f, options)
        print(res)
        # print(res['words_result'][1])
        for item in res['words_result']:
            print(item)
    # 请求百度接口
    def sendFile(self, file):
        APP_ID = '15360113'
        API_KEY = 'xgIGVLwfXVh1Au7GgQsLzKVN'
        SECRET_KEY = 'EveZUPO0TNW7D0EIzOQBtaC4besprl6W'
        options = {}
        # options["recognize_granularity"] = "big"
        options["language_type"] = "CHN_ENG"
        options["detect_direction"] = "true"
        options["detect_language"] = "true"
        # options["vertexes_location"] = "true"
        # options["probability"] = "true"
        res = AipOcr(APP_ID, API_KEY, SECRET_KEY).general(file, options)
        return res

# 读取文件
def get_file_content(filePath):
    # print(filePath)
    with open(filePath, "rb") as fp:
        return fp.read()
